package com.medicine.hbp.adapter;

import org.kymjs.kjframe.http.HttpParams;

import com.medicine.hbp.base.MyBaseAdapter;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.FansInfoVo;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.R;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
 * @author Administrator
 *
 */
public class CareforTwoAdapter extends MyBaseAdapter{
	private LayoutInflater inflater;
	private Handler handler;
	public CareforTwoAdapter(Activity activity, Handler handler) {
		this.activity=activity;
		this.handler=handler;
		inflater=LayoutInflater.from(activity);
	}

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		ViewHolder holder;
		if(view==null){
			holder=new ViewHolder();
			view=inflater.inflate(R.layout.item_carefor, null);
			holder.textcare_name=(TextView) view.findViewById(R.id.textcare_name);
			holder.textcare_qm=(TextView) view.findViewById(R.id.textcare_qm);
			holder.btn_guanzhu=(Button) view.findViewById(R.id.btn_guanzhu);
			view.setTag(holder);
		}else{
			holder=(ViewHolder) view.getTag();
		}
		final FansInfoVo infoVo=(FansInfoVo) getItem(position);
		if(infoVo!=null){
			holder.textcare_name.setText(infoVo.userName);
			holder.textcare_qm.setText(infoVo.userSignature);
			if(null!=infoVo.userIsFocus&&infoVo.userIsFocus){
				holder.btn_guanzhu.setText("�ѹ�ע");
			}else{
				holder.btn_guanzhu.setText("δ��ע");
			}
			holder.btn_guanzhu.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					if(!infoVo.userIsFocus){
						httpAttSubmit(infoVo.userId,position);
					}else{
						http(infoVo.userId);
					}
				}
			});
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					StartIntent.startUser(activity, infoVo.userId);
				}
			});
		}
		return view;
	}
	class ViewHolder{
		TextView textcare_name;
		TextView textcare_qm;
		Button btn_guanzhu;
	}
	/**
	 * ���ڹ�ע�����û�
	 */
	private void httpAttSubmit(int id,final int position) {
		HttpParams params = new HttpParams();
		params.put("userId", id);
		AppHttps.HttpPost(activity,HttpInterface.ATT_SUBMIT, params,
				new AppHttpCallBack<Object>(Object.class) {
					@Override
					public void onSuccess(Object result) {
						ToastUtil.showShort(activity, "��ע�ɹ�");
						notifyDataSetChanged();
					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(activity, errorMsg);
					}
				});

	}
	private void http(final int userid) {
		HttpParams params = new HttpParams();
		params.put("userId", userid);
		AppHttps.HttpPost(activity,HttpInterface.DEL_ATTUSER, params,
				new AppHttpCallBack<Object>(Object.class) {
					@Override
					public void onSuccess(Object result) {
						ToastUtil.showShort(activity, "ȡ����ע�ɹ�");
						Message msg = new Message();
						msg.arg1 = userid;
						msg.arg2 = 1;
						handler.sendMessage(msg);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(activity, errorMsg);
					}
				});
	}

}
